package com.ty.consumer;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.ty.dto.OrderDate;
import com.ty.dto.OrderOk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;

@Configuration
@Slf4j
public class DelayConsumer2 {

//    private static final Logger log = LoggerFactory.getLogger(DelayConsumer.class);

    //定义一个队列 持久化 Q1
    @Bean
    public Queue Delay2(){
        return QueueBuilder.durable("Delay2").build();
    }

    //定义一个交换机Direct-ex  持久化 Q1
    @Bean
    public CustomExchange DelayExchange2(){

        HashMap<String, Object> map = MapUtil.of("x-delayed-type", "fanout");
        return new CustomExchange("delay-order-ex","x-delayed-message",true,false, map);
    }

    //队列和交换机的绑定关系
    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(Delay2()).to(DelayExchange2()).with("").noargs();
    }

    //@RabbitListener(queues = "Delay2")
    public void consumer1(OrderDate msg, Message message, Channel channel) throws IOException {
        System.out.println("consumer2接收到消息："+msg);
        log.debug("消费者->{}", JSONUtil.toJsonStr( msg));
    }



}
